package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.User;
import com.lin.services.StaffService;
import com.lin.services.UserService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * ClassName:StaffController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/20
 * ♣♣♣  员工管理
 */
@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;


    @MyLog("查询员工信息")
    @RequiresPermissions("stuff:stuff:list")
    @RequestMapping(value = "/toStaff", method = RequestMethod.GET)
    public String toStaff(HttpServletRequest request) {
        request.setAttribute("ddId", "Staff");
        List<Map<String, Object>> roleList = staffService.listRoles();
        request.setAttribute("roles", roleList);
        return "staffMain";
    }

    @RequiresPermissions("stuff:stuff:list")
    @RequestMapping(value = "/getStaffInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getStaffInfo(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchName = request.getParameter("stuffSearchName");
        String searchRoleId = request.getParameter("stuffSearchRoleId");
        List<User> list = staffService.listStaffInfo(searchName, searchRoleId);
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("stuff:stuff:update")
    @RequestMapping(value = "/getUserRole", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUserRoleByUserId(HttpServletRequest request) {
        String id = request.getParameter("id");
        return staffService.getUserRoleById(id);
    }

    @MyLog("修改员工信息")
    @RequiresPermissions("stuff:stuff:update")
    @RequestMapping(value = "/editStuff", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateStuff(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        return staffService.updateStuff(parameterMap);
    }

    @MyLog("切换账号状态")
    @RequiresPermissions("stuff:stuff:update")
    @RequestMapping(value = "/changeAccountStatus", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateAccountStatus(HttpServletRequest request) {
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        return staffService.updateAccountStatus(id, status);
    }

    @MyLog("删除员工信息")
    @RequiresPermissions("stuff:stuff:delete")
    @RequestMapping(value = "/deleteStaff", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteStaff(HttpServletRequest request) {
        String id = request.getParameter("id");
        return staffService.deleteStuff(id);
    }

    @RequiresPermissions("stuff:stuff:insert")
    @RequestMapping(value = "/listRoles", method = RequestMethod.POST)
    @ResponseBody       
    public Map<String, Object> listRoles(HttpServletRequest request) {
        List<Map<String, Object>> list = staffService.listRoles();
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        resultMap.put("code", true);
        resultMap.put("msg", "success");
        for (Map<String, Object> map : list) {
            map.put("value", map.get("id"));
            map.put("name", map.get("role"));
            map.put("selected", false);
            map.put("disabled", false);
            map.remove("id");
            map.remove("role");
            mapList.add(map);
        }
        resultMap.put("data", mapList);
        return resultMap;
    }

    @MyLog("添加新员工")
    @RequiresPermissions("stuff:stuff:insert")
    @RequestMapping(value = "/insertStaff", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertStaff(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        return staffService.insertStaff(parameterMap);
    }
}
